layui.config({
    base : "/js/"
}).extend({
    "mframe" : "mframe"
})

layui.use(['form','layer','mframe'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        mframe = layui.mframe,
        $ = layui.jquery;
    
    var prefix = ctx + "system/menu";
    
    //关闭点击
    $(".close").click(function(e){
    	var index = parent.layer.getFrameIndex(window.name);
    	parent.layer.close(index);
    })
    
    //显示上级树
    $(".treeName").click(function(e){
    	var treeId = $("#treeId").val();
    	var menuId = treeId > 0 ? treeId : 1;
    	var url = prefix + "/selectMenuTree/" + menuId;
    	var height = $(window).height() - 150;
    	 layui.layer.open({
             type: 2,
     		 maxmin: true,
             shade: 0.3,
             title: "菜单选择",
             fix: false,
             area: [ '480px', height + 'px'],
             content: url,
             shadeClose: true,
             btn: ['确认', '关闭'],
             yes: function (index, layero) {
            	 doSubmit(index,layero)
             }, 
             cancel: function () {
                 return true;
             }
         });
    })
    
    function doSubmit(index, layero){
    	var body = layui.layer.getChildFrame('body', index);
		$("#treeId").val(body.find('#treeId').val());
		$("#treeName").val(body.find('#treeName').val());
		layui.layer.close(index);
	}
    
    form.on('radio(rchange)', function(data){
    	if (data.value == "M") {
            $("url").parents(".layui-row").hide();
            $("#perms").parents(".layui-row").hide();
            $("#icon").parents(".layui-row").show();
        } else if (data.value == "C") {
        	$("#url").parents(".layui-row").show();
            $("#perms").parents(".layui-row").show();
            $("#icon").parents(".layui-row").hide();
        } else if (data.value == "F") {
        	$("#url").parents(".layui-row").hide();
            $("#perms").parents(".layui-row").show();
            $("#icon").parents(".layui-row").hide();
        }
	});  
    
    form.on("submit(add)",function(data){
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        debugger;
        $.ajax({
    		type : "POST",
    		url : prefix+"/add",
    		data : {
    			"parentId": data.field.parentId,
				"treeName": data.field.treeName,
				"menuName": data.field.menuName,
				"menuType": data.field.menuType,
				"orderNum": data.field.orderNum,
				"icon": data.field.icon,
				"perms": data.field.perms,
				"url": data.field.url
    		},
    		success : function(data) {
    			top.layer.close(index);
                top.layer.msg(data.msg);
                //刷新父页面
                parent.location.reload();
    		}
    	});
        return false;
    })
})